// JORGE FERNANDO GOMEZ
// 1259371
// 189631
// LEC A1
// MARTIN MÜLLER
// D02
// YANG LIU


#include "util.h"



/********************************************
 *                                          *
 *           UTILITY FUNCTIONS              *
 *                                          *
 ********************************************/


/* clear() takes a position index in Figures and resets its deltas
 * array to reset. */
void clear ( Figure * Figures, int position, double reset ) {
    memset(Figures[position].dx, reset, sizeof(double)*MAX_SEGMENTS);
    memset(Figures[position].dy, reset, sizeof(double)*MAX_SEGMENTS);

    Figures[position].drawCount = 0;
}

/* update() updates information in Figures at index position with
 * parameters found in string.  Integer type represents different types
 * of parameters. */
void update ( char * string, Figure * Figures, int position, int type ) {

    int drawCount;
    double * doublesPair = malloc( sizeof(double)*2 );


    /* Get parameters and store them accordingly. */
    if ( type == 2 ) {
        doublesPair = getDoubles(string, type, 2);
        Figures[position].x = *doublesPair;
        Figures[position].y = *(doublesPair+1);
    }
    else if ( type == 1 ) {
        doublesPair = getDoubles(string, type, 2);
        drawCount = Figures[position].drawCount;
        Figures[position].dx[drawCount] = *doublesPair;
        Figures[position].dy[drawCount] = *(doublesPair+1);
        Figures[position].drawCount++;
    }

    free(doublesPair);

}

/* getindex() retrieves the index associated with nameString
 * inside Figures, and returns it.  Otherwise -1 is returned. */
int getindex ( char * nameString, Figure * Figures ) {

    size_t length = strlen(nameString);

    int i;
    for ( i = 0; i < MAXFIGS; i++ ) {
        if ( strncmp(nameString, Figures[i].figureName, length) == 0 ) {
            return i;
        }
    }

    return -1;
}

/* exists() looks for nameString inside Figures.
 * It returns 1 if nameString was found, or 0 otherwise. */
int exists ( char * nameString, Figure * Figures ) {

    size_t length = strlen(nameString);

    int i;
    for ( i = 0; i < MAXFIGS; i++ ) {

        if ( !(Figures[i].isdefined) )
            continue;

        if (strncmp(nameString,
                    Figures[i].figureName, length) == 0 )
            return 1;

    }

    return 0;
}

/* getDoubles() skips blocks2skip blocks of non-whitespace
 * characters, and returns a double * doubles, which holds
 * two double numbers. */
double* getDoubles ( char * string, int blocks2skip, int count ) {

    if ( count == 0 )
        return (double *) 0;

    char * nextblock;       /* Pointer for strtod. */

    double * doubles = malloc( sizeof(double)*2 );

    int start = 0;          /* Keeps track of position on string. */
    int blocksFound = 0;
    int incrementFlag = 1;

    while ( 1 ) {
        if ( isspace(string[start]) ) {

            /* Stop when enough blocks have been skipped. */
            if ( blocksFound == blocks2skip )
                break;

            if ( !incrementFlag )
                incrementFlag++;

            start++;
        }
        if ( !isspace(string[start]) ) {
            if ( incrementFlag ) {
                blocksFound++;
                incrementFlag--;
            }
            start++;
        }
    }

    doubles[0] = strtod( string+start, &nextblock );

    int i;
    for ( i = 1; i < count; i++ ) {
        doubles[i] = strtod( nextblock, &nextblock );
    }

    return doubles;
}

/* substr() takes string and creates a new string containig the characters
 * of string from index offset to index length.
 * Returns the resulting substring or the NULL pointer if string is smaller
 * than the provided offset + length */
char* substr (const char * string, int offset, int length) {

    int strLength = strlen(string);
    char * substring = malloc( sizeof(char)*(length+1) );

    if ( strLength < (offset + length) )
        return(NULL);

    int i, j;
    for ( i = offset, j = 0; i < (offset+length); i++, j++) {
        substring[j] = string[i];
    }

    substring[length] = '\0';
    return substring;
}

/* chompim() takes lineString as argument and removes
 * any trailing whitespace from the string */
void chompim (char * lineString) {
    int len = strlen(lineString);
    int offset = 1;

    while ( isspace(lineString[len - offset]) )  {
        offset++;
    }

    lineString[len - offset + 1] = '\0';
}
